//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		action:'order.detail',
		account:app.setting.account,
		show_coupon:true,
		pagetitle:'订单详情',
		
	},
	onReady: function (){
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (opt){
		var page=this;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		var route=page.route;
		page.setData({
			route:route,
		});
		if(route){
			var path=route.slice(0,-6).split('/');
			app.access.plugin=path.shift();
			app.access.action=path.join('.');
		}
		if(opt){
			app.access.query =app.util.made_query(opt,true);
			app.access.op=opt.op?opt.op:'';
		}
		app.util.get(app.access,function(pagedata){
			console.log('orderdetail');console.log(pagedata);
			page.setData({
				pagedata:pagedata
			});
			page.get_hot_goods();
		});
	},
	del: function (e) {
		var pagedata=this.data.pagedata;
		var data = e.currentTarget.dataset;
		app.util.post('jy_weishop/order.detail/delete/id='+data.id);
		this.setData({
				pagedata:pagedata,
		});
		
	},
	cancel:function(){
		app.util.post('jy_weishop/order.detail/cancel/id='+this.data.pagedata.order.id);
	},
	confirm: function (e) {
		var pagedata=this.data.pagedata
		var data = e.currentTarget.dataset;
		pagedata.order.status=1;
		app.util.post('jy_weishop/order.detail/confirm/id='+data.id);
		this.setData({
				pagedata:pagedata,
		});
	},
	show_verify: function (e) {
		var pagedata=this.data.pagedata
		var data = e.currentTarget.dataset;
		data.item=data.item?data.item:{};
		var show_verify_code=data.item.verifycode?data.item.verifycode:data.item.verify_code
		this.setData({
				show_verify_ewm:data.item.appcode?data.item.appcode:pagedata.order.appcode,
				show_verify_code:show_verify_code?show_verify_code:pagedata.order.selfnumber,
				isshow_verify:true
		});
	},
	down_verify: function () {
		this.setData({
				isshow_verify:false
		});
	},
	closed_coupon:function(){
		this.setData({
			show_coupon:false
		});
	},
	makePhoneCall:function(e){
		var data=e.currentTarget.dataset;
		wx.makePhoneCall({
		  phoneNumber: data.mobile
		})
	},
	get_hot_goods: function (){
		var page=this;
		app.util.get('jy_weishop/get_data/gethot_goods/',function(res){
			//console.log('coupon');console.log(pagedata);
			page.setData({
				'pagedata.hot_list':res.list,
			});
		});
	},
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	//唤醒订阅授权
	sendsubmsg: function (orderid) {
			var page=this
			var pagedata=page.data.pagedata
			if(pagedata.tmplIds && pagedata.tmplIds[0]){
				console.log('sendsubmsg');
				app.util.getsubmsgSet(pagedata.tmplIds,function(ShowSubMsg){
					if(ShowSubMsg){
						page.SubscribeMessage(orderid);
					}else{
						console.log('getsubmsgSet fail：'+ShowSubMsg);
						app.util.msg('下单成功！','','jy_weishop/order.list/');
					}
				});
			}else{
				app.util.msg('下单成功！','','jy_weishop/order.list/');
			}
	},
	closepaymodal:function(){
		var page=this
		page.setData({
			showpaymodal:false
		});
	},
	pay_credit:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_credit(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				if(res.type=='success'){
					page.sendsubmsg(pagedata.order_id);
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open)
	},
	pay_wechat:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_wechat(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				if(res.type=='success'){
					page.sendsubmsg(pagedata.order_id);
					//app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open);
	},
	pay_alipay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_alipay(pagedata.paymodal.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open);
	},
    pay_ttpay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_ttpay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open)
	},
	pay_kspay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_kspay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open)
	},
	pay_hdfk:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_hdfk(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				if(res.type=='success'){
					page.sendsubmsg(pagedata.order_id);
					//app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		})
	},
	pay_apipay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_apipay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				if(res.type=='success'){
					page.sendsubmsg(pagedata.order_id);
				}else{
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}
		},pagedata.paymodal.di_open,pagedata.paymodal.api_name)
	},
	radiotype:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		the.setData({
			'pagedata.paymodal.paytype':data.type,
			'pagedata.paymodal.api_name':data.name
		})
	},
	radiodi_open:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		var di_open=pagedata.paymodal.di_open;
		di_open=di_open==data.type?'':data.type;
		if(!di_open){
			data.difee=0;
		}
		the.setData({
			'pagedata.paymodal.di_open':di_open,
			'pagedata.paymodal.di_fee':data.difee
		})
	},
	new_gotopay:function(e){
		var the=this;
		var pagedata=the.data.pagedata
		var paytype=pagedata.paymodal.paytype;
		if(paytype=='credit'){
			the.pay_credit();
		}else if(paytype=='wechat'){
			the.pay_wechat();
		}else if(paytype=='alipay'){
			the.pay_alipay();
		}else if(paytype=='hdfk'){
			the.pay_hdfk();
		}else if(paytype=='apipay'){
			the.pay_apipay();
		}else if(paytype=='ttpay'){
			the.pay_ttpay();
		}else if(paytype=='kspay'){
			the.pay_kspay();
		}else{
			app.util.msg('请选择支付方式！');
		}
	},
	peerpay:function(){
		var page=this
		var pagedata=page.data.pagedata
		pagedata.post.peerpay=true;
		app.util.post('jy_weishop/order.create/post/',pagedata.post);
	},
	setpay:function(e){
		var page=this
		var data=e.currentTarget.dataset;
		app.util.post('jy_weishop/order.detail/setpay/id='+data.id,'',function(payid){
			app.util.pay(payid,function(res){		
				if(res.type=='more'){
					page.setData({
						showpaymodal:true,
						'pagedata.paymodal':res
					});
				}else if(res.type=='success'){
					app.util.msg(res.message,'','jy_weishop/order.list/');
				}else{
					app.util.msg(res.message);
				}
			});
		});
	}
});